package top.aylan.org.tools;
import org.springframework.web.multipart.MultipartFile;
import top.aylan.org.Common.SystemConstants;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2018/12/23.
 */
public class FileUploader {

    private String rootPath = SystemConstants.UPLOAD_ROOT_PATH;

    public Result upload(MultipartFile file,Directory dir){

        StringBuffer sb = new StringBuffer();
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        String  month = String.valueOf(calendar.get(Calendar.MONTH)+1);

        switch (dir){
            case ART:
                sb.append("art/");
                sb.append(year);
                sb.append("/");
                sb.append(month);
                sb.append("/");
                break;
            case IMAGE:
                sb.append("image/");
                sb.append(year);
                sb.append("/");
                sb.append(month);
                sb.append("/");
                sb.append(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
                sb.append("/");
                break;
            case SYSTEM:
                sb.append("system/");
                sb.append(year);
                sb.append("/");
                break;
            case CATEGORY:
                sb.append("category/");
                sb.append(year);
                sb.append("/");
                break;
            default:
                sb.append("system/");
                sb.append(year);
                sb.append("/");
                sb.append(month);
                sb.append("/");
                break;
        }

        return doUpload(file,sb.toString());
    }

    public Result upload(MultipartFile file,String path){

      return doUpload(file,path);
    }

    public Result doUpload(MultipartFile file, String targetFilePath){

        Result result = new Result();
        if(file != null && !file.isEmpty()){
            String fileName = file.getOriginalFilename();

            String type = fileName.substring(fileName.lastIndexOf("."),fileName.length());
            long time = System.currentTimeMillis();
            String saveFile = targetFilePath+String.valueOf(time)+type;
            File targetFile = new File(rootPath+saveFile);

            if(!targetFile.exists()){
                targetFile.mkdirs();
            }


            try {
                file.transferTo(targetFile);
                result.setCode((byte) 1);
                result.setMessage("上传成功");
                Map<String,Object> map = new HashMap<String ,Object>();
                map.put("path",saveFile);
                result.setData(map);
            } catch (IOException e) {
                result.setCode((byte)-1);
                result.setMessage(e.getMessage());
            }
        }else{
            result.setCode((byte) 2);
            result.setMessage("请上传图片");
        }
        return result;
    }

}
